package com.woniuxy.three.pms.config;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.woniuxy.three.pms.filter.SystemLogoutFilter;
import com.woniuxy.three.pms.filter.UserFormAuthenticationFilter;
import com.woniuxy.three.pms.shiro.UserCacheRealm;
import com.woniuxy.three.pms.shiro.UserRealm;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy;
import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.woniuxy.three.pms.shiro.RootRealm;

import javax.servlet.Filter;

/**
 * 
 * @ProjectName:Pms
 * @Date:2019年6月18日
 * @ClassName:Shiroconfig.java
 * @Author:ThinkPad
 * @Description:TODO shiro配置文件
 * @Version:版本
 * @See：参考版本
 *
 */
@Configuration
public class Shiroconfig {

	/**
	 * 
	 * @Method : getShiroFilterFactoryBean
	 * @Description: 注册过滤器
	 * @Author ThinkPad 作者
	 * @Version 1.0
	 * @param @param  securityManager
	 * @param @return 参数
	 * @return ShiroFilterFactoryBean 返回类型
	 * @Exception
	 * @Date 2019年6月18日
	 */
	@Bean(name = "shiroFilterFactoryBean")
	public ShiroFilterFactoryBean getShiroFilterFactoryBean(
			@Qualifier("defaultWebSecurityManager") DefaultWebSecurityManager securityManager
		) {

		ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
		// 注册安全管理器
		shiroFilterFactoryBean.setSecurityManager(securityManager);

		shiroFilterFactoryBean.setLoginUrl("/hotel/html/login.html");
		shiroFilterFactoryBean.setUnauthorizedUrl("/hotel/html/login.html");

		Map<String, Filter> filters = shiroFilterFactoryBean.getFilters();
		filters.put("authc",new UserFormAuthenticationFilter());
		filters.put("logout",new SystemLogoutFilter());
		shiroFilterFactoryBean.setFilters(filters);
		Map<String, String> fmap = new LinkedHashMap<String, String>();
		fmap.put("/**","anon");
		fmap.put("/html/login.html", "anon");
		fmap.put("/hotel/html/login.html", "anon");
		fmap.put("/root/**", "anon");
		fmap.put("/login", "anon");
		fmap.put("/reg", "anon");
		fmap.put("/css/*", "anon");
		fmap.put("/js/*", "anon");
		fmap.put("/front/js/**", "anon");
		fmap.put("/front/css/**", "anon");
		fmap.put("/front/html/login.html", "anon");
		fmap.put("/front/html/register.html", "anon");
		fmap.put("/**/login/**", "anon");
		fmap.put("/*/login/**", "anon");
		fmap.put("/front/lay/**", "anon");
		fmap.put("/front/scss/**", "anon");
		fmap.put("/front/vendors/**", "anon");
		fmap.put("/front/font/**", "anon");
		fmap.put("/front/fonts/**", "anon");
		fmap.put("/front/image/**", "anon");
		fmap.put("/front/images/**", "anon");
		fmap.put("/hotel/css/**", "anon");
		fmap.put("/hotel/js/**", "anon");
		fmap.put("/hotel/img/*", "anon");
		fmap.put("/html/reg.html", "anon");
		fmap.put("/hotel/html/login.html", "anon");
		fmap.put("/druid", "anon");

		fmap.put("/user/logout", "logout");
		fmap.put("/root/logout", "logout");
		
		shiroFilterFactoryBean.setFilterChainDefinitionMap(fmap);

		return shiroFilterFactoryBean;
	}

	/**
	 * 
	 * @Method : getdefaultWebSecurityManager
	 * @Description: 安全管理器
	 * @Author ThinkPad 作者
	 * @Version 1.0
	 * @param @param  myrelam
	 * @param @return 参数
	 * @return DefaultWebSecurityManager 返回类型
	 * @Exception
	 * @Date 2019年6月18日
	 */

	@Bean(name = "defaultWebSecurityManager")
	public DefaultWebSecurityManager getdefaultWebSecurityManager(@Qualifier("modulauthenticator")ModularRealmAuthenticator authenticator) {
		DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
		securityManager.setAuthenticator(authenticator);
		return securityManager;
	}

	
	  @Bean(name = "modulauthenticator")
	  public ModularRealmAuthenticator getModularRealmAuthenticator(@Qualifier("rootRealm")RootRealm rootRealm,@Qualifier("userRealm") UserRealm userRealm,@Qualifier("userCacheRealm") UserCacheRealm userCacheRealm) {
		  ModularRealmAuthenticator authenticator = new ModularRealmAuthenticator(); 
		  // 认证策略
	      authenticator.setAuthenticationStrategy(new AtLeastOneSuccessfulStrategy());
	      List<Realm> realms = new ArrayList<Realm>(); 
		  realms.add(userRealm);
		  realms.add(rootRealm);
		  realms.add(userCacheRealm);
		  authenticator.setRealms(realms);
	      return authenticator;
	  }
	 

	@Bean(name = "credentialsMatcher")
	/**
	 * 
	 * @Method : gethashedCredentialsMatcher
	 * @Description: 这是凭证管理器
	 * @Author ThinkPad 作者
	 * @Version 1.0
	 * @param @return 参数
	 * @return HashedCredentialsMatcher 返回类型
	 * @Exception
	 * @Date 2019年6月18日
	 */
	public HashedCredentialsMatcher gethashedCredentialsMatcher() {
		HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
		credentialsMatcher.setHashAlgorithmName("MD5");
		credentialsMatcher.setHashIterations(1024);
		credentialsMatcher.setStoredCredentialsHexEncoded(true);
		return credentialsMatcher;

	}

	/**
	 * 
	 * @Method : getrealm
	 * @Description: 安全数据源
	 * @Author ThinkPad 作者
	 * @Version 1.0
	 * @param @param  credentialsMatcher
	 * @param @return 参数
	 * @return UserRealm 返回类型
	 * @Exception
	 * @Date 2019年6月18日
	 */


	@Bean(name = "userRealm")
	public UserRealm getrealm(@Qualifier("credentialsMatcher") HashedCredentialsMatcher credentialsMatcher) {
		UserRealm userRealm = new UserRealm();
		userRealm.setCredentialsMatcher(credentialsMatcher);
		return userRealm;
	}

	@Bean(name = "rootRealm")
	public RootRealm getRootRealm(@Qualifier("credentialsMatcher") HashedCredentialsMatcher credentialsMatcher) {
		RootRealm rootRealm = new RootRealm();
		rootRealm.setCredentialsMatcher(credentialsMatcher);
		return rootRealm;
	}

	@Bean(name = "userCacheRealm")
	public UserCacheRealm getUserCacheRealm(){
		UserCacheRealm userCacheRealm = new UserCacheRealm();
		return userCacheRealm;
	}


}
